package top.wilsonlv.jaguar.cloud.upms.feign;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.wilsonlv.jaguar.cloud.upms.sdk.feign.RemoteUserService;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.UserVO;
import top.wilsonlv.jaguar.cloud.upms.service.PersonalService;
import top.wilsonlv.jaguar.cloud.upms.service.UserService;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;
import top.wilsonlv.jaguar.security.model.SecurityUser;

import javax.validation.constraints.NotBlank;

/**
 * @author lvws
 * @since 2021/6/29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/feign/user")
public class RemoteUserServiceImpl implements RemoteUserService {

    private final PersonalService personalService;

    private final UserService userService;


    @Override
    @GetMapping("/loadUserByUsername")
    public JsonResult<SecurityUser> loadUserByUsername(@RequestParam @NotBlank String username) {
        SecurityUser securityUser = personalService.loadUserByUsername(username);
        if (securityUser == null) {
            return JsonResult.success(null);
        }
        return JsonResult.success(securityUser);
    }

    @Override
    @GetMapping("/loadUsersByDeviceUid")
    public JsonResult<SecurityUser[]> loadUsersByDeviceUid(@RequestParam @NotBlank String deviceUid,
                                                           @RequestParam(required = false) Long userId) {
        return JsonResult.success(personalService.loadUsersByDeviceUid(deviceUid, userId));
    }

    @Override
    @GetMapping("/loadUserByUserId")
    public JsonResult<UserVO> loadUserByUserId(@RequestParam Long userId,
                                               @RequestParam(required = false, defaultValue = "false") Boolean withUserPermission,
                                               @RequestParam(required = false, defaultValue = "false") Boolean withUserFace) {
        return JsonResult.success(userService.getDetail(userId, withUserPermission, withUserFace));
    }

}
